/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.listener;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import nhadat.common.CommonConstant;
import nhadat.xml.DatabaseAndXMLFileMappingSyncrolization;

/**
 *
 * @author Xe Bay
 */
public class NhaDatDatabaseXMLSynchrolizeThread extends Thread {

    private boolean bRun;
    private String contextRealPath;
    private ServletContext context;

    public void setContext(ServletContext context) {
        this.context = context;
    }

    public void setbRun(boolean bRun) {
        this.bRun = bRun;
    }

    public void setContextRealPath(String contextRealPath) {
        this.contextRealPath = contextRealPath;
    }

    @Override
    public void run() {
        bRun = true;
        while (bRun) {
            try {
                String xmlTinDangFilePath = contextRealPath + CommonConstant.TINDANG_XML_FILE_PATH;
                String xmlKhuVucFilePath = contextRealPath + CommonConstant.KHUVUC_XML_FILE_PATH;
                String xmlLoaiTinFilePath = contextRealPath + CommonConstant.LOAITIN_XML_FILE_PATH;
                String xmlLoaiNhaFilePath = contextRealPath + CommonConstant.LOAINHA_XML_FILE_PATH;
                String xmlLoaiGiayToFilePath = contextRealPath + CommonConstant.LOAIGIAYTO_XML_FILE_PATH;
                String xmlTinDangFullFilePath = contextRealPath + CommonConstant.TINDANGFULL_XML_FILE_PATH;
                String xmlKhuvucFullFilePath = contextRealPath + CommonConstant.KHUVUC_FULL_XML_FILE_PATH;
                String xmlLoaiNhaFullFilePath = contextRealPath + CommonConstant.LOAINHA_FULL_XML_FILE_PATH;
                String xmlLoaiTinFullFilePath = contextRealPath + CommonConstant.LOAITIN_FULL_XML_FILE_PATH;
                String xmlLoaiGiayToFullFilePath = contextRealPath + CommonConstant.LOAIGIAYTO_FULL_XML_FILE_PATH;

                // kiem tra file co ton tai ko
                File xmlFile = new File(xmlTinDangFullFilePath);
                DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
                // neu ton tai, dong bo du lieu tu xml => database
                if (xmlFile.exists()) {
                    syn.InsertUpdateDelete(xmlTinDangFullFilePath, xmlLoaiNhaFullFilePath, xmlLoaiGiayToFullFilePath,
                            xmlKhuvucFullFilePath, xmlLoaiTinFullFilePath);

                    System.out.println("Dong bo XML TinDang -> Database: Finished!");
                }
                // set lai context param cho chinh xac
                context.setAttribute(CommonConstant.MAX_MATINDANG_DATABASE, syn.getMaTinDangLonNhat());
                context.setAttribute(CommonConstant.MAX_MATINDANG_HIENTAI, syn.getMaTinDangLonNhat());

                // sau do, dong bo du lieu tu database => xml
                syn.getAllTinDangToXML(xmlTinDangFilePath);
                System.out.println("Dong bo Database -> XML TinDang: Finished!");

                syn.getAllKhuVucToXML(xmlKhuVucFilePath);
                syn.getAllLoaiTinToXML(xmlLoaiTinFilePath);
                syn.getAllLoaiNhaToXML(xmlLoaiNhaFilePath);
                syn.getAllLoaiGiayToToXML(xmlLoaiGiayToFilePath);
                syn.createBLANK_TinDangFullXML(xmlTinDangFullFilePath);

                syn.createFullDsKhuVuc(xmlKhuvucFullFilePath);
                syn.createFullDsLoaiGiayTo(xmlLoaiGiayToFullFilePath);
                syn.createFullDsLoaiNha(xmlLoaiNhaFullFilePath);
                syn.createFullDsLoaiTin(xmlLoaiTinFullFilePath);

                System.out.println("Dong bo Database -> XML others: Finished!");

                String xmlReport = contextRealPath + "report/report.xml";
                String xslReport = contextRealPath + "xsl/reportXSL.xsl";
                String foReport = contextRealPath + "report/report.fo";
                String pdfReport = contextRealPath + "report";

                File reportXML = new File(xmlReport);
                if (!reportXML.exists()) {
                    syn.createBLANK_ReportXML(xmlReport);
                }
                syn.createPDFReportForAdmin(xmlReport, xslReport, foReport, pdfReport);
                System.out.println("Create PDF report: Finished!");

                // sleep thread 5 minutes
                this.sleep(120000);
            } catch (InterruptedException ex) {
                Logger.getLogger(NhaDatDatabaseXMLSynchrolizeThread.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
